


/**
 * 模块依赖
 */
var db = require("../lib/db");

//查找记录
exports.selectFromUserID = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT * FROM notify WHERE fromuserid=? order by operdate desc",
        "args": [userid],
        "handler": handler
    });
};

exports.selectToUserID = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.nickname FROM notify a, viruser b " +
            "WHERE a.expiredate > CURRENT_DATE  AND (a.touserid=b.id OR a.touserid=-1) AND b.id=?   order by createdate desc",
        "args": [userid],
        "handler": handler
    });
};




//插入
exports.insert = function(data, handler) {
    db.execQuery({
        "sql": "insert into notify " +
            "(fromuserid,touserid,createdate,msgcontent,notifytype,expiredate,tonickname,level) values (?,?,now(),?,?,?,?,?)",
        "args":[
            data.fromuserid,
            data.touserid,
            data.msgcontent,
            data.notifytype,
            data.expiredate,
            data.tonickname,
            data.level
        ],
        "handler": handler
    });
};


exports.delete = function(id, handler) {
    db.execQuery({
        "sql": "delete from  notify  where id in (?) ",
        "args":[id],
        "handler": handler
    });
};

exports.deleteByID = function(id, handler) {
    db.execQuery({
        "sql": "delete from  notify  where id in (?) ",
        "args":[id],
        "handler": handler
    });
};




//遍历记录
exports.selectAll = function(handler) {
    db.execQuery({
        "sql": "SELECT a.*,b.nickname FROM notify a,viruser b where a.fromuserid=b.id ORDER BY a.createdate desc,id asc",
        "handler": handler
    });
};
